package cn.xy.org.service.impl;

import cn.xy.basic.exception.BusinessException;
import cn.xy.org.domain.Employee;
import cn.xy.org.domain.Shop;
import cn.xy.org.domain.ShopAuditLog;
import cn.xy.org.mapper.EmployeeMapper;
import cn.xy.org.mapper.ShopAuditLogMapper;
import cn.xy.org.mapper.ShopMapper;
import cn.xy.org.service.IShopService;
import cn.xy.basic.service.impl.BaseServiceImpl;
import cn.xy.org.vo.ShopVo;
import cn.xy.user.domain.Logininfo;
import cn.xy.user.domain.User;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xy
 * @since 2022-06-12
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;

    @Autowired
    private JavaMailSender javaMailSender;

    /**
     * 店铺入驻
     * @param shop
     */
    @Override
    public void settlement(Shop shop) {
        //抽取工具类
        Employee admin = shop.getAdmin();
        //判断参数是否为空,为空就抛异常，不为空就进行下一步
        if(StringUtils.isEmpty(shop.getName()) || StringUtils.isEmpty(shop.getAddress())
                                                || StringUtils.isEmpty(admin.getUsername())){


            throw new BusinessException("参数不能为空");
        }
        //参数不为空，则判断是否注册过
        //通过店铺名字查询数据库是否存在店铺
        Shop shopTmp = shopMapper.loadByName(shop.getName());
        //如果shopTmp不为空，则说明数据库已存在，则此店铺已被注册
        if(shopTmp!=null){
            throw new BusinessException("店铺已注册");
        }

        //判断人是否被注册
        Employee empTmp = employeeMapper.loadByUsername(admin.getUsername());
        //判断人员不为空，并且此人员对应的店铺id也不为空，
        if(empTmp!=null && empTmp.getShop_id()!=null){
            throw new BusinessException("此用户已注册");
        }
        //如果用户对象为空，则说明可以注册
        if(empTmp==null){
            employeeMapper.save(admin);
            empTmp = admin;
        }
        shop.setAdminId(empTmp.getId());
        shopMapper.save(shop);
        empTmp.setShop_id(shop.getId());
        employeeMapper.update(empTmp);

    }

    /**
     * 审核通过
     * @param log
     */
    @Override
    @Transactional
    public void pass(ShopAuditLog log) {
        //修改店铺状态为通过
        Shop shop = shopMapper.loadById(log.getShopId());
        shop.setState(1);
        shopMapper.update(shop);
        //打印日志
        log.setAuditId(334L);
        shopAuditLogMapper.save(log);

        Employee employee = employeeMapper.loadByShopId(log.getShopId());

        SimpleMailMessage mailMessage = new SimpleMailMessage();
        //设置发送人
        mailMessage.setFrom("2756150053@qq.com");
        //邮件主题
        mailMessage.setSubject("金足印象");
        //邮件内容
        mailMessage.setText("晚上八点，金足印象洗歪脚");
        //收件人
        mailMessage.setTo(employee.getEmail());

        javaMailSender.send(mailMessage);


    }

    /**
     * 审核驳回
     */
    @Override
    @Transactional
    public void reject(ShopAuditLog log){
        //修改状态为驳回
        Shop shop = shopMapper.loadById(log.getShopId());
        shop.setState(-1);
        shopMapper.update(shop);

        //记录日志
        log.setAuditId(334L);
        shopAuditLogMapper.save(log);

        //发送驳回邮件
        //获取用户信息
        Employee employee = employeeMapper.loadByShopId(log.getShopId());
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        //设置发送人
        mailMessage.setFrom("2756150053@qq.com");
        //邮件主题
        mailMessage.setSubject("审核驳回");
        //邮件内容
        mailMessage.setText("店铺信息有误，请修改信息并再次提交审核，");
        //收件人
        mailMessage.setTo(employee.getEmail());

        javaMailSender.send(mailMessage);




    }

    @Override
    public List<ShopVo> queryEcharts() {
        return shopMapper.queryEcharts();
    }


}
